#ifndef CHATSERVER_H
#define CHATSERVER_H

#include <QObject>
#include <qbluetoothserviceinfo.h>
#include <qbluetoothaddress.h>
#include <QtCore/QObject>
#include <QtCore/QList>
#include <QBluetoothServer>
#include <QBluetoothSocket>
#include <QBluetoothLocalDevice>

class chatserver : public QObject
{
    Q_OBJECT
public:
    explicit chatserver(QObject *parent = nullptr);
    ~chatserver();

    /* 开启服务端 */
    void startServer(const QBluetoothAddress &localAdapter = QBluetoothAddress());

    /* 停止服务端 */
    void stopServer();

public slots:
    /* 发送消息 */
    void sendMessage(const QString &message);

    /* 服务端主动断开连接 */
    void disconnect();

    /* 接收到消息信号 */
    void messageReceived(const QString &sender, const QString &message);

    /* 客户端连接信号 */
    void clientConnected(const QString &name);

    /* 客户端断开连接信号 */
    void clientDisconnected(const QString &name);

signals:
    /* 客户端连接信号 */
    void sig_clientConnected(const QString &name);

    /* 客户端断开连接信号 */
    void sig_clientDisconnected(const QString &name);

    /* 接收到消息信号 */
    void sig_messageReceived(const QString &sender, const QString &message);
private slots:

    /* 客户端连接 */
    void clientConnected();

    /* 客户端断开连接 */
    void clientDisconnected();

    /* 读 socket */
    void readSocket();

private:
    /* 使用 rfcomm 协议 */
    QBluetoothServer *rfcommServer;

    /* 服务器蓝牙信息 */
    QBluetoothServiceInfo serviceInfo;

    /* 用于保存客户端 socket */
    QList<QBluetoothSocket *> clientSockets;

    /* 用于保存客户端的名字 */
    QList<QString> socketsPeername;
};

#endif // CHATSERVER_H
